home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / develop / ad2mac.arc / AD2MAC.C < prev    next >
C/C++ Source or Header  |  1989-08-04  |  10KB  |  325 lines

  1. /**************************************************************************/
  2. /*                                      */
  3. /*                                      */
  4. /*    FILENAME:    AD2MAC.C        1.0              */
  5. /*                                      */
  6. /*                                      */
  7. /*    DESCRIPTION:                              */
  8. /*        Retrieve the symbol name and value from the assembly      */
  9. /*        language listing of symbol table which produced by the      */
  10. /*        2500AD assembler.                      */
  11. /*                                      */
  12. /*        This program will create an output macro file which can   */
  13. /*        be used to obtain the symbol information for symbolic      */
  14. /*        debug support on HDS-300 Hardware Development Station.      */
  15. /*        It also has an output format option for use with the      */
  16. /*        Motorola CSIC Development System (CDS) for the same      */
  17. /*        purpose.                          */
  18. /*                                      */
  19. /*                                      */
  20. /*    PROGRAMMER:    FAI LEE TUNG STEINBERG                  */
  21. /*                                      */
  22. /*    DATE:        AUG. 04, 1989                      */
  23. /*                                      */
  24. /*    INPUT FILE:    2500AD assembler listing              */
  25. /*                                      */
  26. /*    OUTPUT FILE:    Symbolic file in HDS-300 format or          */
  27. /*                      in CDS format              */
  28. /*                                      */
  29. /**************************************************************************/
  30.  
  31. /*------------------------------------------------------------------------*/
  32. /*    I N C L U D E    AND   D E F I N E    F I L E S              */
  33. /*------------------------------------------------------------------------*/
  34. #include <stdio.h>            /* Stream functions          */
  35. #include <ctype.h>            /* C macros and constance defines */
  36. #include <string.h>            /* String functions          */
  37. /*------------------------------------------------------------------------*/
  38. #define  HDRFLD "Defined"        /* define the table heading      */
  39. #define  ENDFLD "Lines"         /* define the table ending      */
  40. #define  VALID1  "._"            /* define the valid name      */
  41. #define  VALID2 "._$"            /* define the valid name      */
  42. #define  DELIMITER " ;="        /* define the delimiter value      */
  43. /*------------------------------------------------------------------------*/
  44. #define  MAX      132            /* define max line length      */
  45. #define  LENGTH     8            /* define max symbol length      */
  46. #define  NAMELEN   31            /* define max name length      */
  47. #define  LIMIT        6            /* valid symbolic value line      */
  48. #define  FILELEN   12            /* define the filename length      */
  49. #define  FALSE      (0)            /* Function FALSE value       */
  50. #define  TRUE      (1)            /* Function TRUE value          */
  51. /*------------------------------------------------------------------------*/
  52.  
  53. /*------------------------------------------------------------------------*/
  54. /*    G  L  O  B  A  L     V    A  R  I  A  B  L  E              */
  55. /*------------------------------------------------------------------------*/
  56. FILE    *mf,                /* File pointers          */
  57.     *of;
  58. typedef short    BOOLEAN;        /* bi-valued (TRUE/FALSE)      */
  59.  
  60. /*------------------------------------------------------------------------*/
  61. /*    F  U  N  C  T  I  O  N       D  E  C  L  A  R  A    T  I  O  N  S      */
  62. /*------------------------------------------------------------------------*/
  63. void    putformat();
  64. char    *storelength(char *, int);
  65. char    *storename(char *, int);
  66.  
  67. /*------------------------------------------------------------------------*/
  68. /*              M  A  I  N                      */
  69. /*------------------------------------------------------------------------*/
  70. main(argc, argv)            /* AD2MAC main() function      */
  71. int  argc;                /* Number of command line args      */
  72. char *argv[];                /* Vector of command line args      */
  73. {
  74.     char *mac = ".mac",         /* Macro file extension       */
  75.      *rtype = "r",            /* opens to reading the file only */
  76.      *wtype = "w",            /* opens to writing the file only */
  77.      *filein,            /* store input filename       */
  78.      fileout[FILELEN],        /* store output filename      */
  79.      string[MAX],            /* store a line string          */
  80.      temp[MAX],            /* temporary string storage      */
  81.      *namelegth,            /* truncate the symbol name      */
  82.      *name,             /* store the valid symbol name      */
  83.      val[LENGTH],            /* store symbol value          */
  84.      *format,            /* store format command       */
  85.      *token,            /* string token           */
  86.      *fptr;             /* file pointers          */
  87.  
  88.     int  i,j,m,n,            /* initial counter variables      */
  89.      legth,             /* initial counter variables      */
  90.      char_ct,            /* character counter          */
  91.      c,                /* option flags variables      */
  92.      form_flag,
  93.      opt_flag,
  94.      use_ct;            /* reference counter          */
  95.  
  96.     BOOLEAN  EOF_flag;            /* end of file flag          */
  97.  
  98.     EOF_flag = FALSE;            /* initialization          */
  99.     form_flag = 0;
  100.     opt_flag = 0;
  101. /*------------------------------------------------------------------------*/
  102. /*   Function for all options on command line                  */
  103. /*------------------------------------------------------------------------*/
  104.     strcpy(filein, argv[argc-(argc-1)]);
  105.     mf=fopen(filein, rtype);
  106.     *argv++;
  107.     while ( (c = getopt(argc, argv, "iIcC")) != -1 )
  108.     switch(c)
  109.     {
  110.         case 'c':
  111.         case 'C':
  112.         form_flag = 1;
  113.         break;
  114.         case 'i':
  115.         case 'I':
  116.         opt_flag = 1;
  117.         break;
  118.         default:
  119.         break;
  120.     }
  121.     for (i=0; *filein != '.'; i++)
  122.     {
  123.     fileout[i] = *filein++;
  124.     }
  125.     fileout[i] = '\0';
  126.     strcat(fileout, mac);
  127.     of=fopen(fileout, wtype);
  128. /*------------------------------------------------------------------------*/
  129. /*    Read a line to the buffer, begin to locate the symbol table      */
  130. /*    heading.  If found, begin to extract the valid symbol name and      */
  131. /*    value from the table.  If not found, read the next line       */
  132. /*------------------------------------------------------------------------*/
  133.  
  134.     while ( (fgets(string, MAX, mf) != NULL) )
  135.     {
  136.     fptr=string;
  137.     token = strtok(fptr,DELIMITER);
  138.     if  ( strcmp(token,HDRFLD) == 0) {
  139.  
  140.     /*--------------------------------------------------------------------*/
  141.     /*        Symbol table is located, read a line to the buffer until EOF  */
  142.     /*        flag is set.  Extract the obtainable address symbol name and  */
  143.     /*        value only.                           */
  144.     /*--------------------------------------------------------------------*/
  145.         while ( !EOF_flag ) {
  146.         fgets(string, MAX, mf);
  147.         fptr=string;
  148.         n = 0;
  149.         token = strtok(fptr,DELIMITER);
  150.         if ( isdigit(*token) ) {
  151.             token = strtok(NULL,DELIMITER);
  152.             n = 1;
  153.     /*---------------------------------------------------------------------*/
  154.     /*        The symbol name is truncated and the invalid characters are    */
  155.     /*        replaced with underscore (_)                   */
  156.     /*---------------------------------------------------------------------*/
  157.             if (form_flag == 1)
  158.             {
  159.             namelegth = storelength(token,NAMELEN);
  160.             legth = NAMELEN;
  161.             }
  162.             else
  163.             {
  164.              namelegth = storelength(token,LENGTH);
  165.              legth = LENGTH;
  166.             }
  167.  
  168.             name = storename(namelegth,legth);
  169.     /*---------------------------------------------------------------------*/
  170.     /*        The leading zeros of the symbol value are truncated        */
  171.     /*---------------------------------------------------------------------*/
  172.             token = strtok(NULL,DELIMITER);
  173.             j = 0;
  174.             m = 0;
  175.             while ( isxdigit(*token) )
  176.             {
  177.             if ( *token != '0' )
  178.                 while ( isxdigit(*token) )
  179.                 {
  180.                 val[j] = *token;
  181.                 j++;
  182.                 token++;
  183.                 m++;
  184.                 }
  185.             else
  186.                 token++;
  187.             }
  188.             val[j] = '\0';
  189.     /*---------------------------------------------------------------------*/
  190.     /*    Define the symbol name and value is used or not            */
  191.     /*---------------------------------------------------------------------*/
  192.             token = strtok(NULL,DELIMITER);
  193.             use_ct = 0;
  194.             if ( isdigit(*token) && (*token != NULL) )
  195.             {
  196.             use_ct++;
  197.             }
  198. /*------------------------------------------------------------------------*/
  199. /*    Print Options:                              */
  200. /*        HDS-300 Listing format                      */
  201. /*        CDS Listing format                      */
  202. /*        Used Symbols only                      */
  203. /*------------------------------------------------------------------------*/
  204.             if ( (n > 0) && (m > 0) )
  205.             {
  206.             switch(form_flag)
  207.             {
  208.             case 0:
  209.                 format = " @LS";
  210.                 putformat(opt_flag,use_ct,format,name,val);
  211.                 break;
  212.             case 1:
  213.                 format = "DEFINE";
  214.                 putformat(opt_flag,use_ct,format,name,val);
  215.                 break;
  216.             default:
  217.                 break;
  218.             }
  219.             }
  220.         }
  221.         else
  222.         {
  223.             token = strtok(fptr,DELIMITER);
  224.             if ( strcmp(token,ENDFLD) == 0 )
  225.             {
  226.             EOF_flag = TRUE;
  227.                     }
  228.  
  229.         }
  230.  
  231.         }
  232.  
  233.     }
  234.  
  235.     }
  236. if (feof(mf))
  237.     printf("End of file\n");
  238. else
  239.     printf("Error reading file!!!\n");
  240. printf("***** MACRO LISTING FILENAME ==> %s\n\n", fileout);
  241.  
  242. fclose(mf);
  243. fclose(of);
  244.  
  245. }
  246.  
  247. char *storelength(val,maxlen)
  248. char *val;
  249. int  maxlen;
  250. {
  251.     int  ch_ct;
  252.     char name[MAX];
  253.     char *ptr;
  254.  
  255.     ch_ct = 0;
  256.     while ( *val != NULL)
  257.     if ( ch_ct < maxlen)
  258.     {
  259.         name[ch_ct] = *val;
  260.         val++;
  261.         ch_ct++;
  262.     }
  263.     else
  264.         val++;
  265.     name[ch_ct] = '\0';
  266.     ptr=name;
  267.     return(ptr);
  268. }
  269.  
  270. char *storename(ch,maxlen)
  271. char *ch;
  272. int  maxlen;
  273. {
  274.     int j;
  275.     char name[MAX];
  276.     char *ptr;
  277.  
  278.     j = 0;
  279.     name[j] = *ch;
  280.     while ( (j < maxlen) && (!isspace(name[j])) )
  281.     {
  282.     if (j == 0)
  283.     {
  284.         if ( !((isalpha(name[j])) || (strchr(VALID1, name[j]) != NULL)) )
  285.         {
  286.         name[j] = '_';
  287.         j++;
  288.         }
  289.         else
  290.         {
  291.         j++;
  292.         }
  293.     }
  294.     else if ( !((isalnum(name[j])) || (strchr(VALID2,name[j]) != NULL)) )
  295.          {
  296.          name[j] = '_';
  297.          j++;
  298.          }
  299.          else
  300.          {
  301.          j++;
  302.          }
  303.     }
  304.     name[j] = '\0';
  305.     ptr = name;
  306.     return (ptr);
  307. }
  308.  
  309. void
  310. putformat(opt,use_ct,format,name,val)
  311. int  *val, opt, use_ct;
  312. char *name;
  313. {
  314.     if ( opt == 0 )
  315.     {
  316.     printf("%s %s %s\n", format, name, val);
  317.     fprintf(of, "%s %s %s\n", format, name, val);
  318.     }
  319.     else if ( use_ct >= 1 )
  320.     {
  321.     printf("%s %s %s\n", format, name, val);
  322.     fprintf(of, "%s %s %s\n", format, name, val);
  323.     }
  324.  }
  325.